﻿using Microsoft.AspNetCore.Http;
using Publicuse.Appication;
using Publicuse.Autofac;
using Publicuse.Entity;
using Publicuse.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Application
{
    public class FileApplication
    {
        private static BaesApplication baseApplication;
        private static FileApplication userApplication;

        public static FileApplication GetApplication(ServiceContainer container)
        {
            baseApplication = new BaesApplication(container);
            return userApplication;
        }

        static FileApplication()
        {
            userApplication = new FileApplication();
        }



        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="files"></param>
        /// <param name="path"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        public async Task<CommonResult> UploadImage(IFormFileCollection files, string path, string loginUserCode)
        {
            return await Task.Factory.StartNew(() =>
            {
                CommonResult result = new CommonResult();
                foreach (var formFile in files)
                {
                    var fileext = Path.GetExtension(formFile.FileName.ReplaceStrChar());
                    if (fileext.Contains(".php") || fileext.Contains(".asp") || fileext.Contains(".jsp"))
                    {
                        throw new Exception("当前文件不在允许上传范围内已拦截！");
                    }
                    try
                    {
                        if (formFile.Length > 0)
                        {
                            path = path + "\\Files\\Uploads\\" + DateTime.Now.ToString("yyyyMMdd") + "\\";
                            path = path.Replace("\\", "/"); // Linux系统部署路径需要改成这样
                            if (!FileUtil.ExistsDir(path))
                                FileUtil.CreateDirectory(path);

                            var newfilename = formFile.FileName.ReplaceStrChar().ReplaceStrChar();


                            var filePath = path + newfilename;
                            filePath = filePath.Replace("\\", "/"); // Linux系统部署路径需要改成这样
                            using (var stream = new FileStream(filePath, FileMode.Create))
                            {
                                formFile.CopyTo(stream);
                            }
                            if (ImageUtil.IsImage(filePath)) //如果是图片进行压缩
                            {
                                ImageUtil.Compressc(filePath, filePath);
                            }
                            result.data = "/Files/Uploads/" + DateTime.Now.ToString("yyyyMMdd") + "/" + newfilename;
                        }
                    }
                    catch (Exception ex)
                    {
                        result.msg = ex.Message;
                    }
                }
                return result;
            });
        }
    }
}
